package com.eeepay.eeepay_v2.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.model.IntoPiecesInfo;
import com.eeepay.eeepay_v2.model.UserInfo;
import com.eeepay.eeepay_v2.util.AllUtils;
import com.eeepay.eeepay_v2.util.BtnStyleUtil;
import com.eeepay.eeepay_v2.util.CheckUtils;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.DataUtils;
import com.eeepay.eeepay_v2.util.SPUtils;
import com.eeepay.eeepay_v2.util.StatuUtils;
import com.eeepay.eeepay_v2.view.AreaSelectView;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.util.ScreenSwitch;
import com.eeepay.v2_library.view.HorizontalItemView;
import com.eeepay.v2_library.view.LabelEditText;
import com.eeepay.v2_library.view.TitleBar;

/**
 * Created by Ching on 2016/3/10.
 * 经营信息
 */
public class BisInfoActivity extends ABBaseActivity implements View.OnClickListener, TitleBar.LeftBtnOnClickListener {

    private Button nextBtn;
    private LabelEditText detailAddLet, nameLet;//详细地址,商户名称
    private HorizontalItemView areaHiv, typeHiv, industryHiv;
    private LinearLayout merNameLayout, bisScopeLayout;
    private TitleBar titleBar;
    private AreaSelectView areaPopView;

    private boolean isMerchant = true;//是否商户
    private String merName, area, address, bisType, bisTypeNo, industry, mcc;
    private IntoPiecesInfo intoPieces;
    private Intent intent;
    private Boolean isFirst = true;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_business_information;
    }

    @Override
    protected void initWidget() {

        titleBar = getViewById(R.id.tb_bis_info);
        nextBtn = getViewById(R.id.btn_bisInfo_next);
        //add by zhuangzeqin 2017年4月17日13:37:23 修改按钮样式
        BtnStyleUtil.inintBtnDefaultStyle(mContext,nextBtn);
        detailAddLet = getViewById(R.id.let_bisInfo_add_detail);
        //add by zhuangzeqin 2017年9月25日17:13:27 根据产品需求 商户名称判断是否是汉字并且四个字符以上
        nameLet = getViewById(R.id.let_bisInfo_merchant_name);

        areaHiv = getViewById(R.id.hiv_bisInfo_area);
        typeHiv = getViewById(R.id.hiv_bisInfo_merchant_type);
        industryHiv = getViewById(R.id.hiv_bisInfo_merchant_industry);
        detailAddLet.getEditText().setTextColor(this.getResources().getColor(R.color.titlebar_title_txt_color));

        merNameLayout = getViewById(R.id.ll_bisInfo_merchant_name);
        bisScopeLayout = getViewById(R.id.ll_bisInfo_bis_scope);

        if (!isMerchant) {
            merNameLayout.setVisibility(View.GONE);
            bisScopeLayout.setVisibility(View.GONE);
        }

    }

    @Override
    protected void eventOnClick() {

        intoPieces = SPUtils.getIntoPieces();
        if (intoPieces != null) {
            nameLet.setEditContent(intoPieces.getMerchantName());
            detailAddLet.setEditContent(intoPieces.getAddress());

            typeHiv.setRightText(intoPieces.getBisType());
            industryHiv.setRightText(intoPieces.getIndustry());
            bisType = intoPieces.getBisType();
            bisTypeNo = intoPieces.getBisTypeNo();
            mcc = intoPieces.getMcc();
            industry = intoPieces.getIndustry();
//            typeHiv.setRightText(intoPieces.getBisType());
//            industryHiv.setRightText(intoPieces.getIndustry());

//            bisType = intoPieces.getBisType();
//            mcc = intoPieces.getMcc();
        }

        if (!TextUtils.isEmpty(nameLet.getEditContent())
                && !TextUtils.isEmpty(detailAddLet.getEditContent())) {

            //add by zhuangzeqin 2017年4月17日13:37:23 修改按钮样式
            BtnStyleUtil.settingBtnStyle(mContext,nextBtn);
//            nextBtn.setEnabled(true);
//            nextBtn.setBackgroundResource(R.drawable.icon_next_step);


        }

        nextBtn.setOnClickListener(this);
        titleBar.setLeftOnClickListener(this);
        typeHiv.setOnClickListener(this);
        industryHiv.setOnClickListener(this);

       // CheckUtils.addEditChangeListener(nextBtn, R.drawable.icon_next_step, R.drawable.icon_no_next_step, nameLet, detailAddLet);
        CheckUtils.addEditChangeListener(nextBtn, R.drawable.btn_select_style, R.drawable.btn_select_style, nameLet, detailAddLet);

        areaHiv.setOnClickListener(this);

        areaPopView = new AreaSelectView(mContext);

        //选择地区完成回调
        areaPopView.setOnAreaSelectedListener(new AreaSelectView.OnAreaSelectedListener() {
            @Override
            public void onSelected(String area) {
//                if (!adapter.list.contains(area)) {
//                    if (isSingle) {
//                        adapter.removeAll();
//                    }
//                    adapter.add(area);
                areaHiv.setRightText(area);
                areaPopView.dismiss();
//                } else {
//                    showToast("已存在");
//                }
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_bisInfo_next://下一步
                merName = nameLet.getEditContent();
                if (TextUtils.isEmpty(merName)) {//不能非空
                    showToast("请填写有效的商户名称");
                    return;
                }
                //add by zhuangzqin 2017年9月25日17:59:56 判断字段真实长度的实例(中文2个字符,英文1个字符)
                if (!AllUtils.isChineseStr(merName)) {
                    showToast("商户名称，必须为中文汉字");
                    return;
                }
                else {
                    int i = AllUtils.String_length(merName);
                    Log.d("String_length", i + "");
                    if (i < 8) {
                        showToast("商户名称，不能少于4个中文汉字");
                        return;
                    }
                }
                //add by zhuangzeqin 2017年9月26日12:22:49 判断商户名称是否等于用户姓名，如果等于则提示“商户名称不能与用户姓名一致”；
                String userName = UserInfo.getUserInfo2SP().getUserName();
                if(userName.equals(merName))
                {
                    showToast("商户名称不能与用户姓名一致");
                    return;
                }
                getCurrentValue();
                finish();
                ScreenSwitch.switchActivity(this, SettlementActivity.class, null, 0);
                break;
            case R.id.hiv_bisInfo_area://所在地区
//                Bundle bundle = new Bundle();
//                bundle.putBoolean(Constant.IS_SINGLE, true);
//                goActivityForResult(MerchantAreaActivity.class, bundle, Constant.RequestCode.area_all);
                showPopWindow();
                break;
            case R.id.hiv_bisInfo_merchant_type://商户类型
                ScreenSwitch.switchActivity(this, SimpleListActivity.class, null, Constant.RequestCode.SET_MER_TYPE);
                break;
            case R.id.hiv_bisInfo_merchant_industry://二级分类
                intent = new Intent();
                intent.putExtra(Constant.KEY.MERTYPE, typeHiv.getRightText());
                intent.setClass(this, MccActivity.class);
                startActivity(intent);
                break;
        }
    }


    private void showPopWindow() {
        if (areaPopView == null) {
            areaPopView = new AreaSelectView(mContext);
        }
//        WindowManager.LayoutParams params = getWindow().getAttributes();
//        params.alpha = 0.5f;
//        getWindow().setAttributes(params);
        areaPopView.showAtLocation(areaHiv, Gravity.BOTTOM, 0, 0);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case Constant.RequestCode.area_all://所在地区
                String areaStr = data.getStringExtra("area");
                areaHiv.setRightText(areaStr);
                break;
//            case Constant.RequestCode.SET_MER_TYPE:
//                merType = data.getStringExtra("merType");
//                typeHiv.setRightText(merType);
//                mcc = data.getStringExtra("mcc");
//                break;
        }
    }

    public void getCurrentValue() {

        merName = nameLet.getEditContent().trim();
        area = areaHiv.getRightText().trim();
        address = detailAddLet.getEditContent().trim();
//        bisTypeNo = DataUtils.getInstance().getMerTypeNo();
//        bisType = DataUtils.getInstance().getMerType();
//        mcc = DataUtils.getInstance().getMcc();
//        industry = DataUtils.getInstance().getIndustry();
//        merType = typeHiv.getRightText();
//        bisTypeNo = DataUtils.getInstance().getMerTypeNo();
//        bisType = DataUtils.getInstance().getMerType();
//        mcc = DataUtils.getInstance().getMcc();
//        industry = DataUtils.getInstance().getIndustry();


        intoPieces.setMerchantName(merName);
        intoPieces.setAddress(address);

        intoPieces.setBisType(bisType);
        intoPieces.setBisTypeNo(bisTypeNo);
        intoPieces.setMcc(mcc);
        intoPieces.setArea(area);
        intoPieces.setIndustry(industry);

        SPUtils.saveIntoPieces(intoPieces);
        Log.d("requireItem", " bisType " + intoPieces.getBisType() + " bisTypeNo " + intoPieces.getBisTypeNo()
                + " Industry " + intoPieces.getIndustry() + " mcc " + intoPieces.getMcc()
        );
        String status;
        if (!TextUtils.isEmpty(merName) && !TextUtils.isEmpty(address)) {
            status = "已完成";
        } else {
            status = "未完成";
        }
        StatuUtils.getInstance().setBisInfo(status);
        Intent intent = new Intent();
        intent.putExtra("status", status);
        setResult(RESULT_OK, intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //设置选择的商户类别
        if (intoPieces != null && isFirst) {
//            typeHiv.setRightText(intoPieces.getBisType());
//            industryHiv.setRightText(intoPieces.getIndustry());
//            bisType = intoPieces.getBisTypeNo();
//            mcc = intoPieces.getMcc();
            isFirst = false;
        } else {
            bisType = DataUtils.getInstance().getMerType();
            industry = DataUtils.getInstance().getIndustry();
            if (!TextUtils.isEmpty(bisType)) {
                typeHiv.setRightText(bisType);
                bisTypeNo = DataUtils.getInstance().getMerTypeNo();
            }
            if (!TextUtils.isEmpty(industry)) {
                industryHiv.setRightText(industry);
                mcc = DataUtils.getInstance().getMcc();
            }
        }

    }

    @Override
    public void onLeftClick(View view) {
        getCurrentValue();
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { //按下的如果是BACK，同时没有重复
            onLeftClick(null);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
